﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using System.Data;
using System.Data.SqlClient;
namespace Manager
{
    /// <summary>
    /// HW_Manger_Admin_User业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_Manger_Admin_UserManager
    {
        			
        #region##通过主键ID得到HW_Manger_Admin_User
        /// <summary>
        /// 功能：通过ID得到HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_Manger_Admin_User GetById(string xx_username)
        {            
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.GetById(xx_username);
			}
        }
        /// <summary>
        /// 功能：通过ID得到HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public int ResetPassWord(string xx_username,string pass)
        {
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService();
                    HW_XXX_UserService service = new HW_XXX_UserService();
                    TShowPassWordService tservice = new TShowPassWordService();
                    service.ResetPassword(trans,xx_username, pass);
                    
                    //string pass=System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    int n = ws.ResetPassword(trans,xx_username, pass);
                    tservice.Reset(trans,xx_username,pass);
                    trans.Commit();
                    return n;

                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        /// <summary>
        /// 重置密码，并且记录明文密码
        /// </summary>
        /// <param name="xx_username">用户名</param>
        /// <param name="showpass">明文密码</param>
        /// <param name="dispass">加密密码</param>
        /// <returns></returns>
        public int ResetPassWord(string xx_username,string showpass, string pass)
        {
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService();
                    HW_XXX_UserService service = new HW_XXX_UserService();
                    TShowPassWordService tservice = new TShowPassWordService();
                    service.ResetPassword(trans, xx_username, pass);

                    //string pass=System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    int n = ws.ResetPassword(trans, xx_username, pass);
                    tservice.Reset(trans, xx_username, showpass);
                    trans.Commit();
                    return n;

                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
		
		#region##通过主键ID删除HW_Manger_Admin_User
        /// <summary>
        /// 功能：通过主键ID删除HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string xx_username)
		{
			using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.DeleteById(xx_username);
			}
		}
		#endregion			
		
        #region##添加HW_Manger_Admin_User
        /// <summary>
        /// 功能：添加HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_Manger_Admin_User hw_manger_admin_user)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
                HW_Admin_DetailService detailservice = new HW_Admin_DetailService();
                SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    ws.Add(trans, hw_manger_admin_user);
                    int n= detailservice.Add(trans, hw_manger_admin_user.hW_Admin_Detail);
                    trans.Commit();
                    return n;
                }
                catch(Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
			}
        }
        public int Add(HW_Manger_Admin_User hw_manger_admin_user,TShowPassWord entity)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                TShowPassWordService tservice = new TShowPassWordService();
                HW_Admin_DetailService detailservice = new HW_Admin_DetailService();
                HW_XXX_UserService xxuserService = new HW_XXX_UserService();
                HW_XXX_User xxuser=new HW_XXX_User ()
                {
                    XX_UserName=hw_manger_admin_user.XX_UserName,
                    XX_Pass=hw_manger_admin_user.XX_Pass,
                    CNName=hw_manger_admin_user.CNName,
                    ErrorTimes=0,
                    LastLoginTime=null,
                    RoleId=hw_manger_admin_user.RoleId,
                    Version=0
                };
                SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    xxuserService.Add(trans,xxuser);
                    ws.Add(trans, hw_manger_admin_user);
                    int n = detailservice.Add(trans, hw_manger_admin_user.hW_Admin_Detail);
                    tservice.Add(entity, trans);
                    trans.Commit();
                    return n;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
		
		#region##得到HW_Manger_Admin_User集合
		/// <summary>
        /// 功能：得到HW_Manger_Admin_User集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_Manger_Admin_User> GetList()
		{
		    using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.GetList();
			}
		}
		#endregion
		
		#region##修改HW_Manger_Admin_User
        /// <summary>
        /// 功能：修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Manger_Admin_User(HW_Manger_Admin_User hw_manger_admin_user)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.ModHW_Manger_Admin_User(hw_manger_admin_user);
			}
        }
        #endregion
		
		#region##统计HW_Manger_Admin_User
		/// <summary>
        /// 修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_Manger_Admin_User
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_Manger_Admin_User
        public DataTable GetCityAdmin(string pdistrictCode,int level)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetAdminByDisLevel(pdistrictCode, level);
            }
        }
        public DataTable GetAdminByRole(int RoleId)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetAdminByRole(RoleId);
            }
        }
        public DataTable GetAdmin(int pageIndex, int pageSize, out int count, string districtcode, string roleid)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetAdmin(pageIndex, pageSize,out count, districtcode, roleid);
            }
        }
        public DataTable GetAdmin(string districtcode, string roleid)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetAdmin(districtcode, roleid);
            }
        }
        public IList<HW_Manger_Admin_User> GetDistrictAdmin()
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetAdminByLevelBottom(3);
            }
        }
        public DataTable GetDistrictAdmin(int pageIndex, int pageSize, out int count,string districtCode)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetDistrictAdmin(pageIndex, pageSize, out count,districtCode);
            }
        }
        public DataTable GetDistrictAdmin(string pid)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetDistrictAdmin(pid);
            }
        }
        public DataTable GetMyAdmin(int pageIndex, int pageSize, out int count, string districtCode)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetMyAdmin(pageIndex, pageSize,out count,districtCode);
            }
        }
        public DataTable Search(int pageIndex, int pageSize, out int count, string districtcode, string searchString,string roleid)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.Search(pageIndex, pageSize, out count, districtcode,searchString,roleid);
            }
        }
        //public DataTable GetMyAdmin(int pageIndex, int pageSize, out int count, string districtCode,string searchString)
        //{
        //    using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
        //    {
        //        return ws.GetMyAdmin(pageIndex, pageSize, out count, districtCode, searchString);
        //    }
        //}
        public DataTable GetSubAdmin(int pageIndex, int pageSize, out int count,string districtcode)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetSubAdmin(pageIndex, pageSize, out count,districtcode);
            }
        }
        public HW_Manger_Admin_User GetByUserName(string userName)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                return ws.GetByUserName(userName);
            }
        }
        public DataTable SearchRole(int pageIndex, int pageSize, out int count, int roleid, string searchString)
        {
            using (HW_Manger_Admin_UserService ws = new HW_Manger_Admin_UserService())
            {
                if (roleid == 0)
                {
                    if (string.IsNullOrEmpty(searchString))
                    {
                        return ws.SearchRole(pageIndex, pageSize, out count);
                    }
                    else
                    {
                        return ws.SearchRole(pageIndex, pageSize, out count, searchString);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(searchString))
                    {
                        return ws.GetByRole(pageIndex, pageSize, out count, roleid);
                    }
                    else
                    {
                        return ws.SearchRole(pageIndex, pageSize, out count, roleid, searchString);
                    }
                }
            }
        }
        #endregion
    }
}